
var handler_leftClick_01;
var handler_leftClick_02;

var handler_leftClick = {
    leftClick_01:function(){
        /*鼠标点击效果（从全景选片区）*/
        handler_leftClick_01 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
        handler_leftClick_01.setInputAction((movement)=>{
            var entityPicked = viewer.scene.pick(movement);
            window.test = entityPicked;
            if(!entityPicked.id.id){
                return;
            } else{
                id = entityPicked.id.id;
            };
            var layer = scene.layers.find("片区划分_150m");
            layer.setOnlyObjsVisible([], true);
            unitsBillboardClose(entities_QH);
            var billboardArr = [
                {name:"board_BAZX", units: units_BAZX, entities:entities_BAZX},
                {name:"board_DCW", units: units_DCW, entities:entities_DCW},
                {name:"board_GW", units: units_GW, entities:entities_GW},
                {name:"board_QW", units: units_QW, entities:entities_QW},
                {name:"board_MW", units: units_MW, entities:entities_MW},
            ];
            billboardObj = billboardArr.find(item=>{
                return item.name == id
            })
            unitsBillboardShow(billboardObj.units, billboardObj.entities)

            handler_mouseMove.mouseMove_02();
            handler_mouseMove_01 = handler_mouseMove_01 && handler_mouseMove_01.destroy();
            handler_leftClick.leftClick_02();
            handler_leftClick_01 = handler_leftClick_01 && handler_leftClick_01.destroy();
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    },
    leftClick_02:function(){
        /*鼠标点击效果（从片区选单元）*/
        handler_leftClick_02 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        handler_leftClick_02.setInputAction((movement)=>{
            var entityPicked = viewer.scene.pick(movement);
            
            handler_mouseMove_02 = handler_mouseMove_02 && handler_mouseMove_02.destroy();
            handler_leftClick_02 = handler_leftClick_02 && handler_leftClick_02.destroy();
            

            if(!entityPicked.id.id){
                return;
            } else{
                id = entityPicked.id.id;                   
            };
            unitsBillboardClose(entities_GW);
            unitsBillboardClose(entities_QW);
            unitsBillboardClose(entities_MW);
            //关闭单元虚拟框
            var layer = scene.layers.find("version3");
            layer.setOnlyObjsVisible([], true);
            //打开下一级经选效果
            handler_mouseMove.mouseMove_03();
            handler_mouseMove_02 = handler_mouseMove_02 && handler_mouseMove_02.destroy();
            handler_leftClick_02 = handler_leftClick_02 && handler_leftClick_02.destroy();
            //视角飞至单元top-down位置
            var selected_unit;
            units_GW.forEach((item)=>{if(item.id==id){selected_unit=item}});
            units_MW.forEach((item)=>{if(item.id==id){selected_unit=item}});
            units_QW.forEach((item)=>{if(item.id==id){selected_unit=item}});
            viewer.camera.flyTo({
                destination : Cesium.Cartesian3.fromDegrees(selected_unit.X, selected_unit.Y, 2000.0),
                duration:1
            });
            
            /*打开选中片区建筑标签*/
                //选出选中片区的建筑
                selected_buildings =[];
                scene.layers._layers._array.forEach((item)=>{
                    if(item.name.indexOf("@"+characters[parseInt(id)]+"单元")==-1){
                        return;
                    } else{
                        selected_buildings.push(item)
                    };
                })
                //创建标签
                buildingsBillboards = [];
                for(i=0;i<selected_buildings.length;i++){
                    let selected_building = selected_buildings[i];
                    buildingsBillboards[i]=viewer.entities.add({
                        id:selected_building.id,
                        name:selected_building.name,
                        position: Cesium.Cartesian3.fromDegrees(selected_building.lon,selected_building.lat,250),
                        billboard:{
                            image: drawCanvas(img,selected_building.name.split("@")[0],15,70,35),
                            scaleByDistance:new Cesium.NearFarScalar(1000, 1, 100000,0.1),                  //设置随图缩放距离和比例
                            distanceDisplayCondition:new Cesium.DistanceDisplayCondition(50, 100000)        //设置可见距离
                        }
                    })
                }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
    }
}

handler_leftClick.leftClick_01();                                         //状态初始化